//
//  userViewController.m
//  lmq
//
//  Created by 千翼网络科技 on 2017/1/16.
//  Copyright © 2017年 yuanxin. All rights reserved.
//

#import "userViewController.h"
#import "userFirstTableViewCell.h"
#import "userSecondTableViewCell.h"
#import "userThirdTableViewCell.h"
#import "userFourthTableViewCell.h"
#import "userFiveTableViewCell.h"
#import "userSixTableViewCell.h"
#import "keepViewController.h"
#import "MyBecomeManagerViewController.h"
#import "MyPromoteViewController.h"
#import "UIImageView+WebCache.h"
#import "couponViewController.h"
#import "OpinionFeedbackViewController.h"
#import "loginViewController.h"
@interface userViewController ()

@property (nonatomic, strong) NSMutableDictionary *dictOfPic;



@end

@implementation userViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    
   
    

}

- (void)isLogin{
    
    // 登陆判断
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[[UIApplication sharedApplication]keyWindow] animated:YES];
    hud.tag = 500;
    //提示文本
    hud.labelText = @"正在加载...";
    //是否有遮罩
    hud.dimBackground = YES;
    
    NSMutableDictionary *productParams=[[NSMutableDictionary alloc]init];
    [productParams setValue: [PublicFunction getConfigWithKey:@"token"] forKey:@"token"];
    
    [[MNetworkHandler sharedInstance]conURL:Is_Login
                                networkType:NetWorkPOST
                                     params:productParams
                                   delegate:nil
                                    showHUD:NO
                               successBlock:^(id returnData) {
                                   if ([[returnData objectForKey:@"code"]intValue]==1) {
                                       
                                 //      [self creationView];
                                       [hud removeFromSuperview];
                                       
                                       [self creationView];
                                       [self tableViewConfig];
                                   }else{
                                       
                                       [hud removeFromSuperview];
                                       loginViewController *loginVc =[[loginViewController alloc]init];
                                   
                                       loginVc.hidesBottomBarWhenPushed = YES;
                                       
                                       [self.navigationController pushViewController:loginVc animated:YES];
                                   }
                               } failureBlock:^(NSError *error) {
                                   
                                   [hud removeFromSuperview];
                                   NSLog(@"%@",error);
                                   [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
                               }];
}


- (void)creationView{


    self.dictOfPic = [[NSMutableDictionary alloc] init];
    
    NSMutableDictionary *productParams=[[NSMutableDictionary alloc]init];
    NSString *tokenStr=[PublicFunction getConfigWithKey:@"token"];
    NSString *memberId = [PublicFunction getConfigWithKey:@"uid"];
    [productParams setValue:tokenStr forKey:@"token"];
    [productParams setValue:memberId forKey:@"member_id"];
    NSLog(@"==========%@",productParams);
    
    
    [[MNetworkHandler sharedInstance]conURL:User_OrderRedNum
                                networkType:NetWorkPOST
                                     params:productParams
                                   delegate:nil
                                    showHUD:NO
                               successBlock:^(id returnData) {
                                   if ([[returnData objectForKey:@"code"]intValue]==1) {
                                       NSLog(@"success");
                                       NSMutableDictionary *dictRed = [returnData objectForKey:@"data"];
                                       data1 = [[dictRed objectForKey:@"data1"] intValue];
                                       
                                       data2 = [[dictRed objectForKey:@"data2"] intValue];
                                       data3 = [[dictRed objectForKey:@"data3"] intValue];
                                       
                                       [userTableView reloadData];
                                   }
                                   else {
                                       [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:5.0f];
                                   }
                               } failureBlock:^(NSError *error) {
                                   NSLog(@"-------------%@",error);
                                   [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
                               }];

    
    
    
    
    [[MNetworkHandler sharedInstance]conURL:User_Info
                                networkType:NetWorkPOST
                                     params:productParams
                                   delegate:nil
                                    showHUD:NO
                               successBlock:^(id returnData) {
                                   if ([[returnData objectForKey:@"code"]intValue]==1) {
                                       NSLog(@"success");
                                       NSDictionary *dict1=[returnData objectForKey:@"data"];
                                       self.dictOfPic = [returnData objectForKey:@"data"];
//                                       NSString *strPic = @"http://lmq.quyum.cn/";
//                                       
//                                       NSString *strim = [strPic stringByAppendingString:[dict1 objectForKey:@"header_img"]];
//                                       
                                       NSString *urlWare=[dict1 objectForKey:@"header_img"];
                                       NSString *str2=manage(AP_HOST, urlWare);
                                       
                                       strUrl = [str2 stringByReplacingOccurrencesOfString:@"\\" withString:@"/"];
                                       
//                                       [strHeader_img sd_setImageWithURL:[NSURL URLWithString:strUrl] placeholderImage:[UIImage imageNamed:@"tou"]];
                                       
                                       strName= [dict1 objectForKey:@"name"];

                                       strLevel= [dict1 objectForKey:@"level"];
                                       strPhone= [dict1 objectForKey:@"phone"];
                                       userID = [dict1 objectForKey:@"user_id"];
                                       //user_id
                                       NSString *strPoint = @"店主点数:";
                                       point = [strPoint stringByAppendingString:[dict1 objectForKey:@"point"]];
                                       
                                       
                                       strBalance= [dict1 objectForKey:@"balance"];
                                       strIntegral= [dict1 objectForKey:@"integral"];
                                       NSString *str=[NSString stringWithFormat:@"%@", [dict1 objectForKey:@"team"]];
                                       strTeam=str;
                                       strRecord= [dict1 objectForKey:@"record"];
                                       
                                       
                                       
                                       [userTableView reloadData];
                                   }
                                   else if ([[returnData objectForKey:@"code"]intValue]==-1){
                                       [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:5.0f];
                                   }
                               } failureBlock:^(NSError *error) {
                                   NSLog(@"-------------%@",error);
                                   [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
                               }];
    
    
    
    userArr=[[NSMutableArray alloc]init];
    
    NSMutableDictionary *dict1=[[NSMutableDictionary alloc]init];
    [dict1 setObject:@"我的收藏" forKey:@"name"];
    
    NSMutableDictionary *dict2=[[NSMutableDictionary alloc]init];
    [dict2 setObject:@"意见与反馈" forKey:@"name"];
    
    NSMutableDictionary *dict3=[[NSMutableDictionary alloc]init];
    [dict3 setObject:@"如何推广" forKey:@"name"];
    
    //    NSMutableDictionary *dict4=[[NSMutableDictionary alloc]init];
    //    [dict4 setObject:@"联系客服" forKey:@"name"];
    
    [userArr addObject:dict1];
    [userArr addObject:dict2];
    [userArr addObject:dict3];
    //    [userArr addObject:dict4];
    
    
    userTableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT-49-(1))];
    userTableView.delegate=self;
    userTableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    userTableView.dataSource=self;
    [self.view addSubview:userTableView];

    

}
- (void)tableViewConfig {
    
    [userTableView registerClass:[userFirstTableViewCell class]
           forCellReuseIdentifier:@"userFirstTableViewCell"];
    
    [userTableView registerClass:[userSecondTableViewCell class] forCellReuseIdentifier:@"userSecondTableViewCell"];
    
     [userTableView registerClass:[userThirdTableViewCell class] forCellReuseIdentifier:@"userThirdTableViewCell"];
    
     [userTableView registerClass:[userFourthTableViewCell class] forCellReuseIdentifier:@"userFourthTableViewCell"];
    
     [userTableView registerClass:[userFiveTableViewCell class] forCellReuseIdentifier:@"userFiveTableViewCell"];
    
    [userTableView registerClass:[userSixTableViewCell class] forCellReuseIdentifier:@"userSixTableViewCell"];

    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self isLogin];
//    [self creationView];
//    [self tableViewConfig];

    [self.navigationController setNavigationBarHidden:YES animated:NO];
    
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:NO];
}

#pragma mark - 数据源方法
#pragma mark 返回分组数
//指定有多少个分区(Section)，默认为1
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 6;
}
#pragma mark 返回每组行数
//指定每个分区中有多少行，默认为1
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section==4) {
        return 3;
    }
    return 1;
}
#pragma mark - 代理方法
#pragma mark 设置每行高度（每行高度可以不一样）
//改变行的高度（实现这个代理方法后 rowHeight 设定的高度无效）
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==1||indexPath.section==2) {
        return (125);
    }else if (indexPath.section==3){
        return (180);
    }else if (indexPath.section==4){
        return  (45);
    }else if (indexPath.section==0){
        return  (190);
    }
    return (44);
}
#pragma mark返回每行的单元格
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section==0) {

        userFirstTableViewCell *cell
        = [tableView dequeueReusableCellWithIdentifier:@"userFirstTableViewCell" forIndexPath:indexPath];
    
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
        cell.firstViewController=self;
        NSString *strID = @"ID:";
        if (userID.length == 0) {
            
        }else {
            cell.idLabel.text = [strID stringByAppendingString:userID];
        }
        cell.nameLable.text=strName;
        cell.timeLable.text=strLevel;
        cell.dianNumLabel.text = point;
        

        
        [cell.touImageView sd_setImageWithURL:[NSURL URLWithString:strUrl]];
        cell.touImageView.layer.cornerRadius = cell.touImageView.frame.size.width * 0.5;
        cell.touImageView.layer.masksToBounds = YES;

        
        return cell;
    }else if (indexPath.section==1){
        userSecondTableViewCell *cell
        = [tableView dequeueReusableCellWithIdentifier:@"userSecondTableViewCell" forIndexPath:indexPath];

        cell.secondViewController=self;
        
        if (data1 == 0) {
            cell.labelData1.hidden = YES;
        }else {
            
            cell.labelData1.text = [NSString stringWithFormat:@"%ld", (long)data1];
            
        }
        if (data2 == 0) {
            cell.labelData2.hidden = YES;
        }else {
            
            cell.labelData2.text = [NSString stringWithFormat:@"%ld", (long)data2];
            
        }
        if (data3 == 0) {
            cell.labelData3.hidden = YES;
        }else {
            
            cell.labelData3.text = [NSString stringWithFormat:@"%ld", (long)data3];
            
        }
        
        return cell;
        
    }else if (indexPath.section==2){
        userThirdTableViewCell *cell
        = [tableView dequeueReusableCellWithIdentifier:@"userThirdTableViewCell" forIndexPath:indexPath];


        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        cell.thirdViewController=self;
        cell.dfkLabel1.text=strBalance;
        cell.dfkLabel2.text=strIntegral;
//        NSString *str333 = @"  ";//strTeam
        cell.dfkLabel3.text=strTeam;
        cell.dfkLabel4.text=strRecord;
        return cell;
    }else if (indexPath.section==3){
        userFourthTableViewCell *cell
        = [tableView dequeueReusableCellWithIdentifier:@"userFourthTableViewCell" forIndexPath:indexPath];
   
            
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        cell.fourViewController=self;
        return cell;
    }else if (indexPath.section==4){
        userFiveTableViewCell *cell
        = [tableView dequeueReusableCellWithIdentifier:@"userFiveTableViewCell" forIndexPath:indexPath];


        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        cell.scLabel.text=[userArr[indexPath.row] objectForKey:@"name"];
        return cell;
    }else {
        userSixTableViewCell *cell
        = [tableView dequeueReusableCellWithIdentifier:@"userSixTableViewCell" forIndexPath:indexPath];

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.phoneLabel.text=strPhone;
    return cell;
    }
}



//选中Cell响应事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];//选中后的反显颜色即刻消失
    if (indexPath.section==4&&indexPath.row==0) {

        keepViewController *keepVC=[[keepViewController alloc]init];
        keepVC.hidesBottomBarWhenPushed=YES;
        [self.navigationController pushViewController:keepVC animated:YES];
    }
    else if (indexPath.section==4&&indexPath.row==1){
        NSLog(@"意见与反馈");
        OpinionFeedbackViewController *vc = [[OpinionFeedbackViewController alloc] init];
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
        
    }else if (indexPath.section==4&&indexPath.row==2){
        NSLog(@"如何推广");
        MyPromoteViewController *vc = [[MyPromoteViewController alloc] init];
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
        
    }
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
